package com.antoco.controlcenter.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.antoco.controlcenter.iface.IPictListener;

public class GameRotateView extends View implements Runnable, IPictListener {
    private Bitmap bitmap = null;
    private int bitmapWidth = 64;
    private int bitmapHeight = 64;
    private float angle = 0.0f;
    private Matrix matrix = new Matrix();

    public GameRotateView(Context context, int mapBitmap) {
        super(context);
        // TODO Auto-generated constructor stub
        setFocusableInTouchMode(true); //设置可以捕捉键盘事件
        //获取图像资源
        bitmap = ((BitmapDrawable) context.getDrawable(mapBitmap)).getBitmap();
        bitmapWidth = bitmap.getWidth();
        bitmapHeight = bitmap.getHeight();
        new Thread(this).start();
    }

    public void run() {
        // TODO Auto-generated method stub
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO: handle exception
                Thread.currentThread().interrupt();
            }
            postInvalidate(); //可以直接在线程中更新界面
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        matrix.reset();
        matrix.setRotate(angle); //设置旋转
        //按照matrix的旋转构建新的Bitmap
        Bitmap bitmapcute = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
        //绘制旋转之后的图像
        GameRotateView.DrawImage(canvas, bitmapcute, (320 - bitmapWidth) / 2, 10);
        bitmapcute = null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            angle--;
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            angle++;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        // TODO Auto-generated method stub
        return true;
    }

    /**
     * 绘制一个Bitmap
     * canvas  画布
     * bitmap  图片
     * x      屏幕上的x坐标
     * y      屏幕上的y坐标
     */
    public static void DrawImage(Canvas canvas, Bitmap _bitmap, int x, int y) {
        /* 绘制图像 */
        canvas.drawBitmap(_bitmap, x, y, null);
    }

    @Override
    public void onChangeAngle(float angleRoll) {
        angle = angleRoll;
    }

    public IPictListener getIPictListener() {
        return this;
    }
}
